package com.twu.biblioteca.controller;

import com.twu.biblioteca.models.User;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: ema93sh
 * Date: 8/17/13
 * Time: 12:33 AM
 */
public class UserController {
    private final List<User> userList;
    private boolean userLoggedIn;
    private User currentUser;

    public UserController(List<User> userList) {
        this.userList = new ArrayList<User>(userList);
    }

    public boolean login(String username, String password) {
        User user = findUser(username);
        if (user != null && user.checkPassword(password))
        {
            currentUser = user;
            userLoggedIn = true;
        }
        return isUserLoggedIn();
    }

    public User findUser(String username) {
        for (User user: userList)
        {
           if (username.equals(user.getUsername()))
           {
               return user;
           }
        }
        return null;
    }


    public boolean isUserLoggedIn() {
        return userLoggedIn;
    }


    public User getCurrentUser() {
        return currentUser;
    }

    public boolean logout() {
        if (userLoggedIn)
        {
            currentUser = null;
            userLoggedIn = false;
        }
        return !isUserLoggedIn();  //To change body of created methods use File | Settings | File Templates.
    }
}
